﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.IO;

namespace TCPClient
{
    class Program
    {
        private static int m_sendSize = 172032;

        static void Main(string[] args)
        {
            try
            {
                // Create a TcpClient.
                // Note, for this client to work you need to have a TcpServer 
                // connected to the same address as specified by the server, port
                // combination.
                Int32 port = 13000;
                TcpClient client = new TcpClient("172.17.86.45", port);

                NetworkStream stream = client.GetStream();

                FileStream fs = File.OpenRead(args[0]);
                byte[] sendData = new byte[m_sendSize];

                int i = 0;
                long totalBytes = 0;
                while ((i = fs.Read(sendData, 0, sendData.Length)) != 0)
                {
                    totalBytes += i;
                    // Send the message to the connected TcpServer.
                    Console.WriteLine("Sent {0,-12} bytes", i);
                    stream.Write(sendData, 0, i);
                }
                Console.WriteLine("Total Sent {0,-12} bytes", totalBytes);
                // Close everything.
                stream.Close();
                client.Close();
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine("ArgumentNullException: {0}", e);
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: {0}", e);
            }

            Console.WriteLine("\n Press Enter to continue...");
            Console.Read();

        }
    }
}
